package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.Admin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.BaseInfoService;
import com.example.mybatisplus.model.domain.BaseInfo;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-02-28
 * @version v1.0
 */
@Controller
@RequestMapping("/baseInfo")
public class BaseInfoController {

    private final Logger logger = LoggerFactory.getLogger( BaseInfoController.class );

    @Autowired
    private BaseInfoService baseInfoService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        BaseInfo  baseInfo =  baseInfoService.getById(id);
        return JsonResponse.success(baseInfo);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        baseInfoService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateBaseInfo(@PathVariable("id") Long  id, @RequestBody BaseInfo  baseInfo) throws Exception {
        baseInfo.setId(id);
        baseInfoService.updateById(baseInfo);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建BaseInfo
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestBody BaseInfo  baseInfo) throws Exception {
        baseInfoService.save(baseInfo);
        return JsonResponse.success(null);
    }

    @GetMapping("/list")
    @ResponseBody
    public JsonResponse list(@RequestParam(required = false,defaultValue = "1") Integer pageNo,
                             @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        Admin currentUserInfo = SessionUtils.getCurrentUserInfo();
        QueryWrapper<BaseInfo> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_id",currentUserInfo.getId());
        wrapper.eq("user_id",1);
        Page<BaseInfo> page = baseInfoService.page(new Page<>(pageNo, pageSize),wrapper);
        return JsonResponse.success(page);
    }
}

